<template>
	<view>
		<view class="status_bar">
			<!-- 这里是状态栏 -->
		</view>
		<view class="top-bar">
			<view :class="['top-bar-item',Inv==0?'top-bar-item-choose':'']" @click="Inv = 0">全部</view>
			<view :class="['top-bar-item',Inv==1?'top-bar-item-choose':'']" @click="Inv = 1">待支付</view>
			<view :class="['top-bar-item',Inv==2?'top-bar-item-choose':'']" @click="Inv = 2">已完成</view>
		</view>
		<view class="" v-show="Inv == 0">
			<view class="order-item" v-for="Order in Orders">
				<view class="info-container">
					<view>
						<image :src="Order.logo" class="seller-logo"></image>
					</view>
					
					<view style="width: 622rpx;">
						<view>
							<text class="seller-name">{{Order.sellername}}</text>
							<text :class="Order.payStatus == 0?'payStatus0':'payStatus1'">{{Order.payStatus == 0?'待支付':'已完成'}}</text>
						</view>
						<view style="margin-top: 15rpx;">
							<text class="time">{{Order.time}}</text>
							<view style="float: right;">
								<image class="moneyImg" src="../../static/icons/money.png"></image>
								<text class="orderAmount">{{Order.orderAmount}}</text>
							</view>
						</view>
					</view>
				</view>
				
				<view class="btn-container">
					<view></view>
					<view>
						<button @click="toComment(Order)" v-show="Order.payStatus == 1" style="margin-right: 20rpx;border: #00aaff solid 3rpx;border-radius: 2em;" class="mini-btn" type="default" size="mini" plain="true">
							<text class="btn-text">去评价</text>
						</button>
						<button @click="toPay(Order.id)" v-show="Order.payStatus == 0" style="margin-right: 20rpx;border: #00aaff solid 3rpx;border-radius: 2em;" class="mini-btn" type="default" size="mini" plain="true">
							<text class="btn-text">去支付</text>
						</button>
						<button @click="toDetail(Order)" style="border: #00aaff solid 3rpx;border-radius: 2em;" class="mini-btn" type="default" size="mini" plain="true">
							<text class="btn-text">详情</text>
						</button>
					</view>
					
				</view>
			</view>
		</view>
		<view class="" v-show="Inv == 1">
			<view class="order-item" v-for="Order in Orders" v-show="Order.payStatus == 0">
				<view class="info-container">
					<view>
						<image :src="Order.logo" class="seller-logo"></image>
					</view>
					
					<view style="width: 622rpx;">
						<view>
							<text class="seller-name">{{Order.sellername}}</text>
							<text :class="Order.payStatus == 0?'payStatus0':'payStatus1'">{{Order.payStatus == 0?'待支付':'已完成'}}</text>
						</view>
						<view style="margin-top: 15rpx;">
							<text class="time">{{Order.time}}</text>
							<view style="float: right;">
								<image class="moneyImg" src="../../static/icons/money.png"></image>
								<text class="orderAmount">{{Order.orderAmount}}</text>
							</view>
						</view>
					</view>
				</view>
				
				<view class="btn-container">
					<view></view>
					<view>
						<button @click="toPay(Order.id)" v-show="Order.payStatus == 0" style="margin-right: 20rpx;border: #00aaff solid 3rpx;border-radius: 2em;" class="mini-btn" type="default" size="mini" plain="true">
							<text class="btn-text">去支付</text>
						</button>
						<button @click="toDetail(Order)" style="border: #00aaff solid 3rpx;border-radius: 2em;" class="mini-btn" type="default" size="mini" plain="true">
							<text class="btn-text">详情</text>
						</button>
					</view>
					
				</view>
			</view>
		</view>
		<view class="" v-show="Inv == 2">
			<view class="order-item" v-for="Order in Orders" v-show="Order.payStatus == 1">
				<view class="info-container">
					<view>
						<image :src="Order.logo" class="seller-logo"></image>
					</view>
					
					<view style="width: 622rpx;">
						<view>
							<text class="seller-name">{{Order.sellername}}</text>
							<text :class="Order.payStatus == 0?'payStatus0':'payStatus1'">{{Order.payStatus == 0?'待支付':'已完成'}}</text>
						</view>
						<view style="margin-top: 15rpx;">
							<text class="time">{{Order.time}}</text>
							<view style="float: right;">
								<image class="moneyImg" src="../../static/icons/money.png"></image>
								<text class="orderAmount">{{Order.orderAmount}}</text>
							</view>
						</view>
					</view>
				</view>
				
				<view class="btn-container">
					<view></view>
					<view>
						<button @click="toComment(Order)" v-show="Order.payStatus == 1" style="margin-right: 20rpx;border: #00aaff solid 3rpx;border-radius: 2em;" class="mini-btn" type="default" size="mini" plain="true">
							<text class="btn-text">去评价</text>
						</button>
						<button @click="toDetail(Order)" style="border: #00aaff solid 3rpx;border-radius: 2em;" class="mini-btn" type="default" size="mini" plain="true">
							<text class="btn-text">详情</text>
						</button>
					</view>
					
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				Inv: 0,
				ip: "http://localhost:8080",
				Orders: []
			}
		},
		methods: {
			toPay: function(OrderID){
				var that = this
				uni.request({
					url: this.ip + "/orders/pay/" + OrderID,
					success:function(){
						that.getAllOrders()
					}
				})
			},
			// 跳转订单详情页面
			toDetail: function(Order){
				console.log("ready to show Detail : ..."+Order.id)
				uni.navigateTo({
					url: './order_detail',
					// events: {
					// 	toDetail: function(data) {
					// 		console.log(data)
					// 	}
					// },
					/* success: function(res) {
					    // 通过eventChannel向被打开页面传送数据
					    res.eventChannel.emit('showDetail', { data: Order })
					} */
				})
				/*
				 利用监听器进行页面间传值，
				*/
				setTimeout(() => {
					uni.$emit('showDetail', {
						Order: Order
					})
				}, 300)
			},
			toComment: function(Order){
				uni.navigateTo({
					url: './comment'
				})
				setTimeout(() => {
					uni.$emit('goComment', {
						Order: Order
					})
				}, 300)
			},
			string2buffer: function (str) {
			    // 首先将字符串转为16进制
			    let val = ""
			    for (let i = 0; i < str.length; i++) {
			      if (val === '') {
			        val = str.charCodeAt(i).toString(16)
			      } else {
			        val += ',' + str.charCodeAt(i).toString(16)
			      }
			    }
			    // 将16进制转化为ArrayBuffer
			    return new Uint8Array(val.match(/[\da-f]{2}/gi).map(function (h) {
			      return parseInt(h, 16)
			    })).buffer
			  },
			getAllOrders: function(e) {
				var that = this
				this.Inv = 0
				this.Orders = []
				uni.request({
					url: that.ip + "/orders/getAllOrders",
					method: "GET",
					success: function(data) {
						that.Orders = data.data.AllOrderList
						for (var i = 0; i < that.Orders.length; i++) {
							/* var buffer = new ArrayBuffer(that.Orders[i].logo)
							const arrayBuffer = new Uint8Array(buffer)
							const base64 = "data:image/png;base64,"+uni.arrayBufferToBase64(arrayBuffer)
							that.Orders[i].logo = base64
							console.log(that.Orders[i].logo) */
							// console.log(typeof(that.string2buffer(that.Orders[i].logo)))
							// const arrayBuffer = that.string2buffer(that.Orders[i].logo.replace(/[\r\n]/g,""))
							// var order = that.Orders[i].logo
							// var base64 = "data:image/png;base64,"+uni.arrayBufferToBase64(order)
							// console.log(base64)
							that.Orders[i].logo = that.Orders[i].logo.replace(/[\r\n]/g,"")
						}
					}
				})
			}
		},
		onLoad: function() {
			this.getAllOrders()
		},
		onPullDownRefresh: function(){
			this.getAllOrders()
		}
	}
</script>

<style>
	.seller-logo {
		width: 128rpx;
		height: 128rpx;
		border-radius: 1em;
	}
	
	.info-container {
		display: flex;
		flex-direction: row;
		margin-top: 15rpx;
	}
	
	.status_bar {
		height: var(--status-bar-height);
		width: 100%;
		background-color: #FFFFFF;
	}

	.top-bar {
		background-color: #FFFFFF;
		height: 100upx;
		display: flex;
	}

	.top-bar-item {
		font-size: 30upx;
		flex: 1;
		text-align: center;
		color: #C9C9C9;
		height: 100upx;
		line-height: 100upx;
	}

	.top-bar-item-choose {
		color: #00aaff;
		border-bottom: 4upx solid #00aaff;
	}

	.order-item {
		padding: 10rpx;
		margin-bottom: 15rpx;
		background-color: #FFFFFF;
		width: 700rpx;
		margin-left: 15rpx;
		border-radius: 1em;
	}

	.seller-name {
		font-size: 36rpx;
		color: #555555;
		margin-left: 10rpx;
	}

	.payStatus0 {
		font-size: 36rpx;
		color: #4CD964;
		float: right;
		margin-right: 10rpx;
	}

	.payStatus1 {
		font-size: 36rpx;
		color: #999999;
		float: right;
		margin-right: 10rpx;
	}

	.moneyImg {
		width: 22rpx;
		height: 22rpx;
	}

	.orderAmount {
		font-size: 32rpx;
		color: #999999;
		margin-right: 10rpx;
	}

	.time {
		font-size: 32rpx;
		color: #555555;
		margin-left: 10rpx;
	}

	.btn-container {
		display: flex;
		justify-content: space-between;
		/* flex-direction: row-reverse; */
	}

	.btn-text {
		font-size: 26rpx;
		font-weight: bold;
		color: #00aaff;
	}

	page {
		background-color: #fafafa;
	}
</style>
